
*------------------------------------------------------------------------------*
* Prepare the sample
*------------------------------------------------------------------------------*	   
*--- Append all together
use "data/working/2. ethereum.dta",clear
append using "data/working/1. bitcoin.dta"
append using "data/working/3. litecoin.dta"
append using "data/working/4. bitcoin_cash.dta"
append using "data/working/5. zcash.dta"
append using "data/working/6. Dash.dta"
append using "data/working/7. monero.dta"	
append using "data/working/8. xrp.dta"


rename type currency_id


*--- Only use 2018's data
gen year = year(date)
keep if year == 2018 | year == 2017



*------------------------------------------------------------------------------*
* Calculate the abnormal returns
*------------------------------------------------------------------------------*
order currency_id date
sort currency_id date


* Set panel
encode currency_id,gen(dcurrency_id)
xtset dcurrency_id date


* Generate the daily return
gen price_open = l.price
gen return = (price-price_open)/price_open


*------------------------------------------------------------------------------*
* Merge FF factors
*------------------------------------------------------------------------------*
* Merge
rename date trdate
merge m:1 trdate using "data/source/ffall"
unique trdate if _merge == 1 
drop if _merge == 2
sort currency_id trdate


keep currency_id capmvrvcur trdate price price_open return mktrfall rfall _merge dcurrency_id 
gen mktreturn = mktrfall+rfall


*------------------------------------------------------------------------------*
* Generate volatility
*------------------------------------------------------------------------------*

rangestat (sd) return, by(currency_id) interval(trdate -30 -1)
rename return_sd volatility30 

rangestat (sd) return, by(currency_id) interval(trdate -7 -1)
rename return_sd volatility7 


*------------------------------------------------------------------------------*
* Generate Beta
*------------------------------------------------------------------------------*
* Generate excess return
gen excessreturn = return - rfall

* Generate market value weight excess return 
gen value_return_tem = excessreturn * capmvrvcur
bys trdate: egen tcapmvrvcur_tem = sum(capmvrvcur)
bys trdate: egen tvalue_return_tem = sum(value_return_tem)
gen wexcessreturn = tvalue_return_tem / tcapmvrvcur_tem
su wexcessreturn
drop value_return_tem tcapmvrvcur_tem tvalue_return_tem


* Use bitcoin excess return as benchmark
gen excessreturn_bitcoin_tem = excessreturn if currency_id == "bitcoin"
bys trdate: egen excessreturn_bitcoin = max(excessreturn_bitcoin_tem)
drop excessreturn_bitcoin_tem
rangestat (reg) excessreturn excessreturn_bitcoin, by(currency_id) interval(trdate -365 -1)


drop reg_nobs reg_r2 reg_adj_r2 b_cons se_excessreturn_bitcoin se_cons
rename b_excessreturn_bitcoin beta

su beta


*------------------------------------------------------------------------------*
* Generate Momentum
*------------------------------------------------------------------------------*
* Generate the 7,14,21,28 day's price
sort currency_id trdate
xtset dcurrency_id trdate
gen momentum1 = (price - l7.price)/l7.price
gen momentum2 = (price - l14.price)/l14.price
gen momentum3 = (price - l21.price)/l21.price
gen momentum4 = (price - l28.price)/l28.price



rename trdate date
drop _merge
save  "data/working/beta",replace 


